package v3.chapter2.synchronizedBlockLockAll;

public class test2 {

	public static void main(String[] args) {
		// 同一个“对象监视器”
		Service2 service = new Service2();
		MyObject2 object = new MyObject2();

		MyThreadA a = new MyThreadA(service, object);
		a.setName("a");
		a.start();

		MyThreadB b = new MyThreadB(object);
		b.setName("b");
		b.start();

	}

}

class MyThreadA extends Thread {
	private Service2 service;
	private MyObject2 object;

	public MyThreadA(Service2 service, MyObject2 object) {
		super();
		this.service = service;
		this.object = object;
	}

	@Override
	public void run() {
		super.run();
		service.testMethod1(object);
	}
}

class MyThreadB extends Thread {
	private MyObject2 object;

	public MyThreadB(MyObject2 object) {
		super();
		this.object = object;
	}

	@Override
	public void run() {
		super.run();
		object.speedPrintString();
	}
}

class Service2 {
	public void testMethod1(MyObject2 object) {
		synchronized (object) {
			try {
				System.out.println("testMehtod1_getLock time=" + System.currentTimeMillis() + " run ThreadName="
						+ Thread.currentThread().getName());
				Thread.sleep(2000);
				System.out.println("testMehtod1_releaseLock time=" + System.currentTimeMillis() + " run ThreadName="
						+ Thread.currentThread().getName());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyObject2 {
	synchronized public void speedPrintString() {
		System.out.println("speedPrintString_getLock time=" + System.currentTimeMillis() + " run ThreadName="
				+ Thread.currentThread().getName());
		System.out.println("------------------------------------");
		System.out.println("speedPrintString_releaseLock time=" + System.currentTimeMillis() + " run ThreadName="
				+ Thread.currentThread().getName());
	}
}